package com.zui.entity.party;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.zui.entity.BaseAutoIdEntity;
import com.zui.entity.setup.Uom;
import com.zui.security.User;


@Entity
@Table(name="party")
@Inheritance(strategy = InheritanceType.JOINED)
public class Party extends BaseAutoIdEntity {
	private static final long serialVersionUID = 1L;

	private PartyType partyType;
	private Uom preferredCurrencyUOM;
	private List<User> userLongins;
	private List<PartyContactMech> partyContactMechs;

	
	@Transient
	public void addUserLogin(User user) {
		user.setParty(this);
		if(userLongins == null)
			userLongins = new ArrayList<User>();
		userLongins.add(user);
	}
	@Transient
	public void removeUserLogin(User user) {
		if(userLongins == null)
			return;
		this.getUserLongins().remove(user);
		user.setParty(null);
	}
	@Transient
	public void addPartyContactMech(PartyContactMech contactMech) {
		contactMech.setParty(this);
		if(partyContactMechs == null)
			partyContactMechs = new ArrayList<PartyContactMech>();
		partyContactMechs.add(contactMech);
	}
	@Transient
	public void removePartyContactMech(PartyContactMech contactMech) {
		if(partyContactMechs == null)
			return;
		this.getPartyContactMechs().remove(contactMech);
		contactMech.setParty(null);
	}	


	/*------------------------------
	 * getters and annotations
	 --------------------------------*/
	@ManyToOne
	@JoinColumn(name="partyTypeId")
	public PartyType getPartyType() {
		return partyType;
	}
	@ManyToOne
	@JoinColumn(name="preferredUomId")
	public Uom getPreferredCurrencyUOM() {
		return preferredCurrencyUOM;
	}
	
	@OneToMany(mappedBy = "party", fetch = FetchType.LAZY, 
			cascade = {CascadeType.ALL})
	@OrderBy(value = "userName asc")
	public List<User> getUserLongins() {
		return userLongins;
	}
	
	@OneToMany(mappedBy="party", fetch = FetchType.LAZY,
			cascade = {CascadeType.ALL})
	public List<PartyContactMech> getPartyContactMechs() {
		return partyContactMechs;
	}
	
	
	
	public void setUserLongins(List<User> userLongins) {
		this.userLongins = userLongins;
	}
	public void setPartyType(PartyType partyType) {
		this.partyType = partyType;
	}
	public void setPreferredCurrencyUOM(Uom preferredCurrencyUOM) {
		this.preferredCurrencyUOM = preferredCurrencyUOM;
	}
	public void setPartyContactMechs(List<PartyContactMech> partyContactMechs) {
		this.partyContactMechs = partyContactMechs;
	}
}
